package com.hgk.wanwan.main.fragment.persentfragment.alarmclock;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import width.hgk.wanwan.R;

public class EditTimeActivity extends Activity {
    //headerView
    private LinearLayout mLltHdView;
    private TextView mTvTitle;
    private Button mBtnLeft,mBtnRight;
    String str = "";

    private Calendar mCalendar, mAlarmCalendar;
    private Button dateBtn = null;
    private Button timeBtn = null;
    private Button isAlarmBtn = null;
    private EditText mText = null;
    private TextView seemText = null;
    private Button setBtn = null;
    private Button discardBtn = null;
    private LinearLayout mLinearSetTime;
    private ScrollView mSvSeeTime;
    private int year, month, day, hour, minute;
    private DatabaseHelper dbHelper;
    private Date currentTime, beginTime;
    private String click_key = "";
    private int isAlarmNum = 0;
    private long isAlarm = 0;
    private AlarmManager mAlarm;
    private long alarm_id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_edit_time);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        dbHelper = new DatabaseHelper(EditTimeActivity.this, "ideal.sql");
        mCalendar = Calendar.getInstance();
        year = mCalendar.get(Calendar.YEAR);
        month = mCalendar.get(Calendar.MONTH);
        day = mCalendar.get(Calendar.DAY_OF_MONTH);
        hour = mCalendar.get(Calendar.HOUR_OF_DAY);
        minute = mCalendar.get(Calendar.MINUTE) + 5;
        Intent intent = getIntent();
        click_key = intent.getStringExtra("click_key");
        initTimeView();
        initData(intent);

    }



    private void initTimeView() {
        mLltHdView = (LinearLayout) findViewById(R.id.header_edit_time_view);
        initHeaderView(mLltHdView);
        mLinearSetTime = (LinearLayout) findViewById(R.id.llt_edit_time_view_settime);
        mSvSeeTime = (ScrollView) findViewById(R.id.sv_edit_time_view_seetime);
        seemText = (TextView) findViewById(R.id.seemText);//显示框
        dateBtn = (Button) findViewById(R.id.data_settime);//日期按钮
        timeBtn = (Button) findViewById(R.id.Time);//时间按钮
        isAlarmBtn = (Button) findViewById(R.id.isAlarm);//闹铃被选中状态
        mText = (EditText) findViewById(R.id.mText);//编辑框
        setBtn = (Button) findViewById(R.id.set);//设置
        discardBtn = (Button) findViewById(R.id.discard);//取消
    }

    private void initHeaderView(LinearLayout v) {
        mTvTitle = (TextView) v.findViewById(R.id.tv_wherefrag_title);
        mBtnLeft = (Button) v.findViewById(R.id.btn_left_header);
        mBtnRight = (Button) v.findViewById(R.id.btn_right_header);
        mTvTitle.setVisibility(View.VISIBLE);
        if (click_key.equals("click_edit")) {
            str = "备忘详情";
        }else if(click_key.equals("click_havedone")){
            str = "备忘详情";
        }else{
            str = "创建备忘录";
        }
        mTvTitle.setText(str);
        mBtnLeft.setVisibility(View.VISIBLE);
        mBtnLeft.setBackgroundResource(R.drawable.back);
        mBtnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData(Intent intent) {
        if (click_key.equals("click_edit")) {
            mSvSeeTime.setVisibility(View.VISIBLE);
            mLinearSetTime.setVisibility(View.GONE);
            dateBtn.setEnabled(false);
            timeBtn.setEnabled(false);
            currentTime = new Date();
            CharSequence sysTimeStr = DateFormat.format("yyyy年MM月dd日H时mm分",
                    currentTime.getTime());
            EditTimeActivity.this.setTitle(sysTimeStr);

            String text = intent.getStringExtra("text");
            String date_time = intent.getStringExtra("time");
            seemText.setText(text);

            dateBtn.setText("创建时间：");
            timeBtn.setText(date_time);
        } else if (click_key.equals("click_havedone")) {
            mSvSeeTime.setVisibility(View.VISIBLE);
            mLinearSetTime.setVisibility(View.GONE);
            dateBtn.setEnabled(false);
            timeBtn.setEnabled(false);

            String text = intent.getStringExtra("text");
            String date_time = intent.getStringExtra("time");

            seemText.setText(text);
            seemText.setTextColor(Color.GRAY);
            dateBtn.setText("查看时间");
            dateBtn.setTextSize(15);
            timeBtn.setText(date_time);
            timeBtn.setTextSize(12);

        } else {// ���
            mSvSeeTime.setVisibility(View.GONE);
            mLinearSetTime.setVisibility(View.VISIBLE);
            dateBtn.setEnabled(true);
            timeBtn.setEnabled(true);

            Calendar CurrentCalendar = Calendar.getInstance();
            dateBtn.setText(CurrentCalendar.get(Calendar.YEAR) + "年"
                    + (CurrentCalendar.get(Calendar.MONTH) + 1) + "月"
                    + CurrentCalendar.get(Calendar.DAY_OF_MONTH) + "日");

            timeBtn.setText(mCalendar.get(Calendar.HOUR_OF_DAY) + "时"
                    + CurrentCalendar.get(Calendar.MINUTE) + " +5分");

            mAlarmCalendar = Calendar.getInstance();
            dateBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCalendar = Calendar.getInstance();
                    // TODO Auto-generated method stub
                    new DatePickerDialog(EditTimeActivity.this,
                            new DatePickerDialog.OnDateSetListener() {
                                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                    mAlarmCalendar.set(Calendar.YEAR, year);
                                    mAlarmCalendar.set(Calendar.MONTH,
                                            monthOfYear);
                                    mAlarmCalendar.set(Calendar.DAY_OF_MONTH,
                                            dayOfMonth);
                                    EditTimeActivity.this.year = year;
                                    EditTimeActivity.this.month = monthOfYear;
                                    EditTimeActivity.this.day = dayOfMonth;
                                    dateBtn.setText(year + "年" + (month + 1)
                                            + "月" + day + "日");
                                }
                            }, year, month, day).show();
                }
            });
            timeBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    mCalendar = Calendar.getInstance();
                    new TimePickerDialog(EditTimeActivity.this,
                            new TimePickerDialog.OnTimeSetListener() {
                                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                    mAlarmCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                                    mAlarmCalendar.set(Calendar.MINUTE, minute);
                                    mAlarmCalendar.set(Calendar.SECOND, 0);
                                    mAlarmCalendar.set(Calendar.MILLISECOND, 0);
                                    EditTimeActivity.this.hour = hourOfDay;
                                    EditTimeActivity.this.minute = minute;
                                    timeBtn.setText(hourOfDay + "时" + minute + " +5分");
                                }
                            }, hour, minute, false).show();
                }
            });

            isAlarmBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    isAlarmNum++;
                    if (isAlarmNum % 2 == 0) {
                        isAlarmBtn.setBackgroundResource(R.drawable.checknull);
                        isAlarm = 0;
                    } else {
                        isAlarmBtn.setBackgroundResource(R.drawable.check);
                        isAlarm = 1;
                    }
                }
            });

            mAlarmCalendar.set(Calendar.YEAR, year);
            mAlarmCalendar.set(Calendar.MONTH, month);
            mAlarmCalendar.set(Calendar.DAY_OF_MONTH, day);
            mAlarmCalendar.set(Calendar.HOUR_OF_DAY, hour);
            mAlarmCalendar.set(Calendar.MINUTE, minute);
            mAlarmCalendar.set(Calendar.SECOND, 0);
            mAlarmCalendar.set(Calendar.MILLISECOND, 0);

            setBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String mtext = mText.getText().toString();
                    String mtime = year + "年" + (month + 1) + "月" + day + "日"
                            + hour + "时" + minute + "分";
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyy年MM月dd日H时mm分");
                    try {
                        beginTime = sdf.parse(mtime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    currentTime = new Date();
                    if (beginTime.getTime() < currentTime.getTime()) {
                        Toast.makeText(EditTimeActivity.this, "yyyy年MM月dd日H时mm分", Toast.LENGTH_SHORT).show();
                    } else {
                        addData(mtext, mtime, isAlarm);
                        if (isAlarm == 1) {
                            mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
                            Intent intent = new Intent(EditTimeActivity.this,
                                    AlarmReceiver.class);
                            PendingIntent pendingIntent = PendingIntent
                                    .getBroadcast(EditTimeActivity.this, (int) alarm_id, intent, 0);

                            mAlarm.set(AlarmManager.RTC_WAKEUP,
                                    mAlarmCalendar.getTimeInMillis(),
                                    pendingIntent);
                        }
                        Intent mWidgetIntent = new Intent();
                        mWidgetIntent.setAction("com.ideal.note.widget");
                        EditTimeActivity.this.sendBroadcast(mWidgetIntent);
                        finish();
                    }

                }
            });

            discardBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    public void addData(String mtext, String mtime, long isalarm) {
        ContentValues values = new ContentValues();
        values.put("mtext", mtext);
        values.put("mtime", mtime);
        values.put("isalarm", isalarm);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        alarm_id = db.insert("user", null, values);
        db.close();
    }
}
